define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'videocollection/index' + location.search,
                    add_url: 'videocollection/add',
                    edit_url: 'videocollection/edit',
                    del_url: 'videocollection/del',
                    multi_url: 'videocollection/multi',
                    import_url: 'videocollection/import',
                    table: 'video_collection',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                // 表单
                var form = $("form", table.$commonsearch);

                // 设置绑定题库
                $("input[name='course.id']", form).addClass("selectpage").data("source", "course/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc").data("pageSize", 10);

                // 注册事件
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('合集编号')},
                        {field: 'name', title: __('合集名称'), operate: 'LIKE'},
                        {field: 'cover', title: __('封面信息'), formatter: Table.api.formatter.images, operate: 'LIKE'},
                        {field: 'description', title: __('合集简介'), operate: 'LIKE'},
                        {field: 'price', title: __('Price'), operate: 'BETWEEN', searchable: false},
                        // {field: 'course.id', title: __('绑定题库'), visible: false},
                        // {field: 'course.name', title: __('关联题库')},
                        {
                            field: 'status',
                            title: __('上架状态'),
                            formatter: Table.api.formatter.normal,
                            searchList: {'1': __('是'), '2': __('否')}
                        },
                        {
                            field: 'update_time',
                            title: __('Update_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            // 绑定题库格式化输出
            $("#c-course_id").data("format-item", function (row) {
                var name = row.name
                if (row.code != '') {
                    name += '(' + row.code + ')';
                }
                return name;
            });

            Controller.api.bindevent();
        },
        edit: function () {

            // 绑定题库格式化输出
            $("#c-course_id").data("format-item", function (row) {
                var name = row.name
                if (row.code != '') {
                    name += '(' + row.code + ')';
                }
                return name;
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
